#include<bits/stdc++.h>
#define int long long
#define For(i,a,b) for(int i=a;i<=b;++i)
#define Rof(i,a,b) for(int i=a;i>=b;--i)
#define rep(i,a) for(int i=1;i<=a;++i)
#define print(x) printf("%lld\n",(int)x)
#define prn(x) printf("%lld ",(int)x)
using namespace std;
const int N=1e7+5,Mod=1e9+7;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){f=(ch=='-'?-f:f);ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*f;
}
int n,k;
int a[N];
int f[N];
struct XXX{
	int l,r,w;
};
signed main(){
	freopen("knight.in","r",stdin);
	freopen("knight.out","w",stdout);
	n=read(),k=read();
	For(i,1,n) a[i]=read();
	f[0]=0;
	For(i,1,n) f[i]=1e16;
	if(n<=2000){
		For(i,0,n){
			int mx=0;
			For(j,1,k){
				mx=max(mx,a[i+j]);
				f[i+j]=min(f[i+j],f[i]+mx);
			}
		}
		int ans=0;
		int p=1;
		Rof(i,n,1){
			//~ //cout<<f[i]<<" ";
			(ans+=p*f[i])%=Mod;
			p*=23;
			p%=Mod;
		}
		printf("%lld\n",ans);
		return 0;
	}
	deque<XXX>q;
	q.push_back({0,0,0});
	set<int>s;
	For(i,1,n+1){
		XXX y=q.front();q.pop_front();
		s.erase(y.w);
		y.l=max(y.l,i-k),y.w=f[y.l]+a[y.r];
		if(y.l<=y.r) q.push_front(y),s.insert(y.w);
		f[i]=*s.begin();
		int l=i,r=i;
		while(!q.empty()&&a[q.back().r]<=a[i]){
			l=q.back().l;
			s.erase(q.back().w);
			q.pop_back();
		}
		q.push_back({l,r,f[l]+a[i]});
		s.insert(f[l]+a[i]);
	}
	int ans=0;
	int p=1;
	Rof(i,n,1){
		//~ cout<<f[i+1]<<" ";
		(ans+=p*f[i+1])%=Mod;
		p*=23;
		p%=Mod;
	}
	printf("%lld\n",ans);
	return 0;
}
/*
6 3
3 4 1 5 6 8 
*/
